﻿using System;
using System.Globalization;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PrettyFormat
{
  [TestClass]
  public class PrettyTimeFormatProviderTest
  {
    [TestInitialize]
    public void Initialize()
    {
      Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US", false);
    }

    [TestMethod]
    public void Just_Now_Test()
    {
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddSeconds(10));
      Assert.AreEqual(Formats.FriendlyDateTime_just_now, format);

      var format2 = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddSeconds(-10));
      Assert.AreEqual(Formats.FriendlyDateTime_just_now, format2);
    }

    [TestMethod]
    public void Nullable_date_Test()
    {
      DateTime? date = DateTime.Now.AddSeconds(10);
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", date);
      Assert.AreEqual(Formats.FriendlyDateTime_just_now, format);

      DateTime? nullDate = null;
      var formatNUll = string.Format(new PrettyTimeFormatProvider(), "{0:P}", nullDate);
      Assert.AreEqual("", formatNUll);
    }

    [TestMethod]
    public void TimeSpanIsSupported()
    {
      var fourMinutesPast = new TimeSpan(0, -4, 0); // 4 minutes past
      var fourMinutesAgo = string.Format(new PrettyTimeFormatProvider(), "{0:P}", fourMinutesPast);
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_minutes_ago, 4), fourMinutesAgo);

      var fourMinutesForward = new TimeSpan(0, 4, 0); // 4 minutes past
      var inFourMinutes = string.Format(new PrettyTimeFormatProvider(), "{0:P}", fourMinutesForward);
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_in_minutes, 4), inFourMinutes);
    }

    [TestMethod]
    public void Past_Minutes_Test()
    {
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddMinutes(-4));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_minutes_ago, 4), format);
    }

    [TestMethod]
    public void Past_Hours_Test()
    {
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddHours(-4));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_hours_ago, 4), format);
    }

    [TestMethod]
    public void Past_Weeks_Test()
    {
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddDays(-4 * 7));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_weeks_ago, 4), format);
    }

    [TestMethod]
    public void Past_Months_Test()
    {
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddMonths(-4));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_months_ago, 4), format);

      var format2 = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddMonths(-4).AddDays(2));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_months_ago, 4), format2, "+ 2 days => it's the same");

      var format3 = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddMonths(-4).AddDays(-2));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_months_ago, 4), format3, "- 2 days => it's the same");
    }

    // FUTURE TESTS

    [TestMethod]
    public void Next_Minutes_Test()
    {
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddMinutes(4));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_in_minutes, 4), format);
    }

    [TestMethod]
    public void Next_Hours_Test()
    {
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddHours(4));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_in_hours, 4), format);
    }

    [TestMethod]
    public void Next_Weeks_Test()
    {
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddDays(4 * 7));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_in_weeks, 4), format);
    }

    [TestMethod]
    public void Next_Months_Test()
    {
      var format = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddMonths(4));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_in_months, 4), format);

      var format2 = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddMonths(4).AddDays(2));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_in_months, 4), format2, "+ 2 days => it's the same");

      var format3 = string.Format(new PrettyTimeFormatProvider(), "{0:P}", DateTime.Now.AddMonths(4).AddDays(-2));
      Assert.AreEqual(string.Format(Formats.FriendlyDateTime_in_months, 4), format3, "- 2 days => it's the same");
    }
  }
}
